// pages/Pet/NearByStore/NearByStore.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetStore } from '../../../api/pet';
import { throttle } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    queryLongitude: wx.getStorageSync('longitude'),
    queryLatitude: wx.getStorageSync('latitude'),
    selNearByStore: wx.getStorageSync('selNearByStore'),
    dataList: [],
    page: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    storeIdList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.storeIdList){
      this.setData({
        storeIdList: options.storeIdList.split(','),
      })
    }
    this._queryPetStore();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
    })
    this._queryPetStore();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    if(this.data.isReachBottom){
      this._queryPetStore()
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取宠物门店
  _queryPetStore(){
    let _this = this;
    const { queryLongitude , queryLatitude , page } = _this.data;
    queryPetStore({page: page,queryLongitude: queryLongitude,queryLatitude: queryLatitude})
    .then(res => {
      if(res.success){
        var list = _this.data.dataList;
        var arr = [];
        for (let index = 0; index < res.data.content.length; index++) {
          const element = res.data.content[index];
          element.serviceProjectNameList = element.serviceProjectName.split(',');
          if(_this.data.storeIdList.length == 0){
            arr.push(element);
          }else{
            if(_this.data.storeIdList.find(itm => itm == element.id)){
              arr.push(element);
            }
          }
        }
        if(_this.data.page == 0){
          list = arr;
        }else{
          list = list.concat(arr);
        }
        _this.setData({
          dataList: list,
          total: res.data.totalElements,
          page: _this.data.page+1,
        })
        if(_this.data.page * 10 >= res.data.totalElements){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 返回
  back(e){
    let _this = this;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    if(wx.getStorageSync('selNearByStore') == '0'){
      return;
    }else{
      const eventChannel = _this.getOpenerEventChannel()
      eventChannel.emit('selStore', {data: item.id});
      wx.navigateBack();
    }
  }
})